<template>
  <table class="table">
    <tr>
      <td></td>
      <td v-for="(cell,idx) in table_rows" :key="idx">{{cell}}</td>
    </tr>
    <tr v-for="(line,idx) in arr_table" :key="idx">
      <td v-if="idx<table_parts_count">{{idx+1}}</td>
      <td v-if="idx==table_parts_count">汇总</td>
      <td v-for="(cell,idx) in line" :key="idx">{{cell}}</td>
    </tr>
  </table>
</template>

<script>
export default {
  name: "HollandElement",
  props: ["raw"],
  data() {
    return {
      table_parts_count: 0,
      table_rows: null,
      arr_table: null,
      isReady: false
    };
  },
  computed: {
    devMode() {
      return this.$store.state.devMode;
    }
  },
  methods: {
    genTable(raw) {
      let arr_p = [];
      let arr_t = [];
      for (let i = 0; i < raw.length; i++) {
        let add_p = true;
        for (let j = 0; j < arr_p.length; j++) {
          if (arr_p[j] == raw[i].p) {
            add_p = false;
            break;
          }
        }
        if (add_p) {
          arr_p.push(raw[i].p);
        }
        let add_t = true;
        for (let j = 0; j < arr_t.length; j++) {
          if (arr_t[j] == raw[i].t) {
            add_t = false;
            break;
          }
        }
        if (add_t) {
          arr_t.push(raw[i].t);
        }
      }
      let arr_table = [];
      for (let j = 0; j < arr_p.length; j++) {
        let arr_line = [];
        for (let k = 0; k < arr_t.length; k++) {
          for (let i = 0; i < raw.length; i++) {
            if (arr_t[k] == raw[i].t && arr_p[j] == raw[i].p) {
              arr_line.push(raw[i].v);
              break;
            }
          }
        }
        arr_table.push(arr_line);
      }
      //sum
      let arr_line = [];
      for (let k = 0; k < arr_t.length; k++) {
        let val = 0;
        for (let i = 0; i < raw.length; i++) {
          if (arr_t[k] == raw[i].t) {
            val += raw[i].v;
          }
        }
        arr_line.push(val);
      }
      arr_table.push(arr_line);
      this.table_rows = arr_t;
      this.table_parts_count = arr_p.length;
      this.arr_table = arr_table;
      this.isReady = true;
    }
  },
  created() {
    this.genTable(this.raw);
    if (this.devMode) {
      window.console.log(this.table_rows);
      window.console.log(this.arr_table);
      window.console.log(this.raw);
    }
  }
};
</script>